package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Client;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.query.ClientQueryObject;
import cn.wolfcode.crm.query.AssignObject;
import cn.wolfcode.crm.service.IClientService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("client")
public class ClientController {

    @Autowired
    IClientService clientService;

    @RequestMapping("")
    @RequiresPermissions(value = {"客户首页","client:index"} ,logical = Logical.OR)
    public String index(){

        return "client/list";
    }

    @RequestMapping("list")
    @RequiresPermissions(value = {"客户列表","client:list"} ,logical = Logical.OR)
    @ResponseBody
    public PageResult list(ClientQueryObject qo){
        Employee principal = (Employee) SecurityUtils.getSubject().getPrincipal();

        if (!principal.isAdmin()) {
            qo.setTracerId(principal.getId());
            qo.setInputId(principal.getId());
        }

        PageResult result = clientService.query(qo);
        return result;
    }




    @RequestMapping("listData")
    @ResponseBody
    public Object listData( ){
        return clientService.list();
    }

    @RequestMapping("save")
    @RequiresPermissions(value = {"客户保存","client:save"} ,logical = Logical.OR)
    @ResponseBody
    public JSONResult save(Client entity){
        JSONResult result = new JSONResult();
        try{
            clientService.save(entity);
        }catch(Exception e){
            e.printStackTrace();
            result.remark(e.getMessage());
        }
        return result;
    }

    @RequestMapping("update")
    @RequiresPermissions(value = {"客户更新","client:update"} ,logical = Logical.OR)
    @ResponseBody
    public JSONResult update(Client entity){
        JSONResult result = new JSONResult();
        try{
            clientService.update(entity);
        }catch(Exception e){
            e.printStackTrace();
            result.remark("操作失败");
        }
        return result;
    }


    @RequestMapping("delete")
    @RequiresPermissions(value = {"客户删除","client:delete"} ,logical = Logical.OR)
    @ResponseBody
    public JSONResult delete(Long id){
        JSONResult result = new JSONResult();
        try{
            clientService.delete(id);
        }catch(Exception e){
            e.printStackTrace();
            result.remark("操作失败");
        }
        return result;
    }

    //将客户放入资源池
    @RequestMapping("putPool")
    @RequiresPermissions(value = {"将客户放入资源池","client:putPool"} ,logical = Logical.OR)
    @ResponseBody
    public JSONResult putPool(Long id){
        JSONResult result = new JSONResult();
        try{
            clientService.putPool(id);
        }catch(Exception e){
            e.printStackTrace();
            result.remark("操作失败");
        }
        return result;
    }

    //分派客户
    @RequestMapping("sign")
    @RequiresPermissions(value = {"分派客户资源","client:sign"} ,logical = Logical.OR)
    @ResponseBody
    public JSONResult sign(AssignObject ao){
        JSONResult result = new JSONResult();
        try{

            //如果当前登录的是市场专员则把客户指定自己名下
            if (ao.getTracerId() == null) {
                Employee principal = (Employee) SecurityUtils.getSubject().getPrincipal();
                ao.setTracerId(principal.getId());
            }

            clientService.sign(ao);
        }catch(Exception e){
            e.printStackTrace();
            result.remark("操作失败");
        }
        return result;
    }

    /*@RequestMapping("changeState")
    @RequiresPermissions(value = {"转正","client:changeState"} ,logical = Logical.OR)
    @ResponseBody
    public JSONResult changeState(Long id){
        JSONResult result = new JSONResult();
        try{
            clientService.changeState(id);
        }catch(Exception e){
            e.printStackTrace();
            result.remark("操作失败");
        }
        return result;
    }*/
}
